package com.mengxuegu.springboot.Controller;


import com.mengxuegu.springboot.entities.Bill;
import com.mengxuegu.springboot.entities.BillProvider;
import com.mengxuegu.springboot.entities.Provider;
import com.mengxuegu.springboot.mapper.BillMapper;
import com.mengxuegu.springboot.mapper.ProviderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class BillController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    BillMapper billMapper;

    @Autowired
    ProviderMapper providerMapper;

    @GetMapping("/bills")
    public  String list(Bill bill , Map<String , Object> map){
        List<BillProvider> bills = billMapper.getBills(bill);
        List<Provider> providers = providerMapper.getProviders(new Provider());

        map.put("bills",bills);
        map.put("providers",providers);

        map.put("billName",bill.getBillName());
        map.put("pid",bill.getPid());
        map.put("pay",bill.getPay());
        return "bill/list";
    }

    //去添加bill 页面
    @GetMapping("/tobill")
    public  String toaddBill(Map<String ,Object> map){
        List<Provider> providers = providerMapper.getProviders(null);
        map.put("providers",providers);
        return "bill/add";
    }

    //添加bill
    @PostMapping("/bill")
    public String  save(Bill bill ){
        logger.info("添加bill  : "+ bill.toString());
        billMapper.addBill(bill);

        return "redirect:/bills";
    }

    //去修改 和详情 页面
    @GetMapping("/toview/{bid}")
    public String toview(@PathVariable Integer bid ,@RequestParam(value = "type",defaultValue = "view") String type,Map<String,Object> map){
        BillProvider billByBid = billMapper.getBillByBid(bid);
        logger.info("查询getBillByBid  : "+ billByBid);
        List<Provider> providers = providerMapper.getProviders(null);
        map.put("bill",billByBid);
        map.put("providers",providers);
        return "bill/" +type;
    }

    //修改bill
    @PutMapping("/bill")
    public String updateBill(Bill bill){
        logger.info("修改bill     : "+ bill.toString());
        int i = billMapper.updateBill(bill);

        return "redirect:/bills";
    }


    //删除bill

    @DeleteMapping("/bill/{bid}")
    public  String deleteBill(@PathVariable Integer bid){
        int i = billMapper.deteleBillByBid(bid);
        logger.info("删除了bill  : "+ bid);
        return "redirect:/bills" ;
    }
}
